package com.wiloon.androidx.fragment;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.wiloon.androidx.R;
import com.wiloon.androidx.BaseActivity;

import org.apache.log4j.Logger;

/**
 * Created by wangyue on 8/7/2014.
 */

public class FragmentActivity extends BaseActivity {
    Logger logger = Logger.getLogger(FragmentActivity.class);
    private boolean fragmentSwitched = false;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.fragment_activity);

        Button switchFragment = (Button) findViewById(R.id.btnSwitchFragment);
        switchFragment.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentManager fm = getFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();

                Fragment fragment = new FragmentReplacement();
                Fragment fragmentFoo = new FragmentFoo();
                Fragment fragmentBar = new FragmentBar();
                Fragment newFragment;
                if (fragmentSwitched) {
                    newFragment = fragmentFoo;
                    fragmentSwitched = false;
                } else {
                    newFragment = fragmentBar;
                    fragmentSwitched = true;
                }

                ft.replace(R.id.fragmentR, newFragment);
                ft.commit();
            }
        });


        int width = getWindowManager().getDefaultDisplay().getWidth();
        int height = getWindowManager().getDefaultDisplay().getHeight();

        Fragment fragmentReplacement = new FragmentReplacement();
        Fragment fragmentFoo = new FragmentFoo();
        Fragment fragmentBar = new FragmentBar();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragmentR, fragmentReplacement);

//        if (width > height) {
//            logger.debug("w>h");
//            ft.replace(R.id.fragmentListBar, fragmentFoo);
//        } else {
//            logger.debug("w<h");
//
//            //ft.replace(R.id.fragmentListBar, fragmentBar);
//        }
        ft.commit();
    }
}
